/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 13-6-6
 * Time: 上午11:21
 * To change this template use File | Settings | File Templates.
 */
Ext.define('cloud.controller.ArticleCategory', {
    extend: 'Ext.app.Controller',
    config: {

        refs: {
            articlecategory: 'articlecategory',
            articlecategoryadd: 'articlecategoryadd',
            articlecategoryedit: 'articlecategoryedit',
            articlecategory_btn_add: 'articlecategory #articlecategory_btn_add',
            articlecategory_submit: 'articlecategoryadd #articlecategory_submit',
            articlecategory_edit: 'articlecategoryedit #articlecategory_edit',
            articlecategory_del: 'articlecategoryedit #articlecategory_del',
            articlecategorySelect: 'articlecategory #articlecategorySelect',
            articlecategory_search: 'articlecategory #articlecategory_search'

        },
        control: {
            /**
             * show articlecategoryadd page
             */
            articlecategory_btn_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var article_cate_add = this.getArticlecategoryadd()||Ext.create('cloud.view.articlecategory.ArticleCategoryAdd');
                    this.getArticlecategory().push(article_cate_add);
                    Ext.Viewport.unmask();
                }

            },

            /**
             * search call
             */
            articlecategory_search: {
                change: function () {
                    var Store = Ext.getStore('ArticleCategoryStore');
                    var value = this.getArticlecategory_search().getValue();
                    Store.clearFilter();
                    if (value != "") {
                        Store.filter('name', value, true);
                    }
                    Store.load({params: {cadid: localStorage.getItem('cadid')}});
                }
            },
            /**
             * show articlecategoryList
             */
            articlecategorySelect: {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var articlecategory = this.getArticlecategory();
                    var articlecategoryedit = this.getArticlecategoryedit()||Ext.create('cloud.view.articlecategory.ArticleCategoryEdit');
                    articlecategoryedit.setRecord(record);
                    this.getArticlecategory().push(articlecategoryedit);
                    document.getElementById('imghead').src = record.get('iconUrl');
                    Ext.Viewport.unmask();
                }
            },
            /**
             * del articlecategory_image
             */
            articlecategory_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getArticlecategoryedit().submit({
                                url: GLOBAL.api_url + 'column_delete',
                                scope: this,
                                method: 'POST',
                                params: {

                                },
                                success: this.exesucess
                            });
                        }
                    }, this);
                }
            },


            /**
             * add
             */
            articlecategory_submit: {
                tap: function () {
                    var article_name=Ext.getCmp('article_name').getValue();
                    var article_display=Ext.getCmp('article_display').getValue();
                    var article_cadid=Ext.getCmp('article_cadid').getValue();
                    if (Ext.isEmpty(article_name))
                        return Ext.Msg.alert('分类名称');
                    if (Ext.isEmpty(article_display))
                        return Ext.Msg.alert('请输入排序');
                    Ext.Viewport.mask({xtype: 'loadmask'});

                    var backthis = this;
                    var file = document.getElementById('articlecategory_img').files[0];
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    //添加没有图片的咨询
                    if (file == null ||file == undefined) {
                        this.getArticlecategoryadd().submit({
                            url: GLOBAL.api_url + 'column_add',
                            scope: this,
                            method: 'POST',
                            success: this.exesucess
                        });
                        Ext.Viewport.unmask();
                    }else{
                        var formdata = new FormData();
                        formdata.append("Filedata", file);
                        var http = new XMLHttpRequest();
                        if (http.upload && http.upload.addEventListener) {
                            http.onreadystatechange = function (e) {
                                if (this.readyState == 4) {
                                    if (this.status == 200) {
                                        var returnText = Ext.decode(this.responseText);
                                        if (returnText.Variables.Result.code == 0) {
                                            var iconUrl = returnText.Variables.data.attachdir;
                                            Ext.Ajax.request({
                                                url: GLOBAL.api_url + 'column_add',
                                                scope: this,
                                                params: {
                                                    iconUrl: iconUrl,
                                                    name:article_name,
                                                    display:article_display,
                                                    cadid:article_cadid
                                                },
                                                method: 'POST',
                                                success: function (result) {
                                                    var returnText = Ext.decode(result.responseText);
                                                    if (returnText.Variables.Result.code == 0) {
                                                        Ext.getStore('ArticleCategoryStore').load({params: {cadid: localStorage.getItem('cadid')}});
                                                        backthis.getArticlecategory().pop();
                                                    } else {
                                                        Ext.Msg.alert(result.Variables.Message.messagestr);
                                                    }
                                                    Ext.Viewport.unmask();
                                                },
                                                failure: function () {//请求失败时执行操作
                                                    Ext.Viewport.unmask();
                                                    Ext.Msg.alert('请求失败', '服务器维护中....');
                                                }
                                            });
                                        }
                                    }
                                }
                            };
                            http.upload.onerror = function () {//请求失败时执行操作
                                Ext.Viewport.unmask();
                                Ext.Msg.alert('请求失败', '服务器维护中....');
                            }
                        }
                        http.open('POST', GLOBAL.api_url + 'attachment_upload&updatatype=article');
                        http.send(formdata);
                    }
                }
            },
            /**
             * edit articlecategory
             */
            articlecategory_edit: {
                tap: function () {
                    var articleedit_name = Ext.getCmp('articleedit_name').getValue();
                    var articleedit_display=Ext.getCmp('articleedit_display').getValue();
                    var articleadd_ccid=Ext.getCmp('articleadd_ccid').getValue();
                    if (Ext.isEmpty(articleedit_name))
                        return Ext.Msg.alert('请输入名称');
                    if (Ext.isEmpty(articleedit_cadid))
                        return Ext.Msg.alert('请选择开通地区');
                    if (Ext.isEmpty(articleedit_display))
                        return Ext.Msg.alert('请输入排序');

                    var backthis = this;
                    var file = document.getElementById('articlecategory_img').files[0];
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    //添加没有图片的咨询
                    if (file == null ||file == undefined) {
                        this.getArticlecategoryedit().submit({
                            url: GLOBAL.api_url + 'column_update',
                            method: 'POST',
                            scope: this,
                            success: this.exesucess
                        });
                        Ext.Viewport.unmask();
                    }else{
                        var formdata = new FormData();
                        formdata.append("Filedata", file);
                        var http = new XMLHttpRequest();
                        if (http.upload && http.upload.addEventListener) {
                            http.onreadystatechange = function (e) {
                                if (this.readyState == 4) {
                                    if (this.status == 200) {
                                        var returnText = Ext.decode(this.responseText);
                                        if (returnText.Variables.Result.code == 0) {
                                            var iconUrl = returnText.Variables.data.attachdir;
                                            Ext.Ajax.request({
                                                url: GLOBAL.api_url + 'column_update',
                                                scope: this,
                                                params: {
                                                    iconUrl: iconUrl,
                                                    name:articleedit_name,
                                                    display:articleedit_display,
                                                    ccid:articleadd_ccid
                                                },
                                                method: 'POST',
                                                success: function (result) {
                                                    var returnText = Ext.decode(result.responseText);
                                                    if (returnText.Variables.Result.code == 0) {
                                                        Ext.getStore('ArticleCategoryStore').load({params: {cadid: localStorage.getItem('cadid')}});
                                                        backthis.getArticlecategory().pop();
                                                    } else {
                                                        Ext.Msg.alert(result.Variables.Message.messagestr);
                                                    }
                                                    Ext.Viewport.unmask();
                                                },
                                                failure: function () {//请求失败时执行操作
                                                    Ext.Viewport.unmask();
                                                    Ext.Msg.alert('请求失败', '服务器维护中....');
                                                }
                                            });
                                        }
                                    }
                                }
                            };
                            http.upload.onerror = function () {//请求失败时执行操作
                                Ext.Viewport.unmask();
                                Ext.Msg.alert('请求失败', '服务器维护中....');
                            }
                        }
                        http.open('POST', GLOBAL.api_url + 'attachment_upload&updatatype=article');
                        http.send(formdata);
                    }
                }
            }
        }
    },
    exesucess: function (from, result) {
        console.log('aadd')
        if (result.Variables.Result.code == 0) {
            Ext.getStore('ArticleCategoryStore').load({params: {cadid: localStorage.getItem('cadid')}});
            this.getArticlecategory().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    }
})
;
